package com.BlackPhoneModule.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import util.ConnectDB;
import util.UtilDAO;

import com.BlackPhoneModule.bean.BlackPhoneBean;

public class BlackPhoneModuleDao {
	// 数据库连接
	private Connection conn = null;
	// st对象
	private PreparedStatement pt = null;

	private Statement st = null;
	// 结果集
	private ResultSet rs = null;
	// 成功失败标志
	private boolean flag = false;
	// 要执行的语句
	private String sql = "";
	private BlackPhoneBean blackPhoneBean = null;
	private List<BlackPhoneBean> blackPhoneList = null;
	//显示所有黑名单
	public List<BlackPhoneBean> getAllTbBlackPhone(String tableName,String order,String tiaojian){
		try{
			conn = ConnectDB.getSqlServerConnection();
			sql= UtilDAO.getList(tableName, order, tiaojian);
			System.out.println("查询语句"+sql);
			pt = conn.prepareStatement(sql);
			rs = pt.executeQuery();
			while(rs.next()){
				if(blackPhoneList==null){
					blackPhoneList = new ArrayList<BlackPhoneBean>();
				}
				blackPhoneList.add(this.getblackPhoneList(rs));
			}
			
		}catch(Exception e){
			System.out.println("error!!!!!!!!!!");
			return null;
		}finally{
			ConnectDB.closeSqlConnection();
		}
		return blackPhoneList;
	}
	/**
	 * 
	 *@author qingyu zhang
	 *@function:get a object from ResultSet
	 * @param rs
	 * @return
	 *2011-2-17
	 *下午03:03:01
	 *AddressListModule
	 *com.AddressListModule.dao
	 *DirectoryBean
	 */
	public BlackPhoneBean getblackPhoneList(ResultSet rs){
		BlackPhoneBean blackPhone = new BlackPhoneBean();
		try{
			blackPhone.setCreateTime(rs.getString("createTime"));
			blackPhone.setId(rs.getInt("id"));
			blackPhone.setPhone(rs.getString("phone"));
			blackPhone.setUserid(rs.getInt("userid"));
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			return blackPhone;
		}
	}
	
	/**
	 * 单条记录删除
	 * jhc
	 * 2011-1-27 下午02:30:38
	 */
	public boolean del(String tableName,String keyId,String idList){
		boolean bo = UtilDAO.del(tableName, keyId, idList);
		return bo ;
	}
	/**
	 * 多项删除
	 * jhc
	 * 2011-1-27 下午02:30:38
	 */
	public boolean delin(String tableName,String keyId,String idList){
		boolean bo = UtilDAO.delin(tableName, keyId, idList);
		return bo ;
	}
	public boolean saveBlackPhoneList(List<BlackPhoneBean> blackphoneList){
		boolean bo = false;
		try
		{
			conn = ConnectDB.getSqlServerConnection();
			st = conn.createStatement();
			if(blackphoneList!=null&&blackphoneList.size()>0){
				for(BlackPhoneBean blackphone:blackphoneList){
					String hql = "insert into tb_blackPhone (phone,createTime,userid) " +
					"values('"+blackphone.getPhone()+"','"+blackphone.getCreateTime()+"',"+blackphone.getUserid()+")";
					System.out.println("hql==="+hql);
					st.addBatch(hql);
				}
				st.executeBatch();
				bo = true;
			}
		}catch(Exception e){
			e.printStackTrace();
			bo = false;
		}finally{
			ConnectDB.closeSqlConnection();
			return bo ;
		}
	}
}
